------------------ fgui require start ------------------
local GuildAddresSelectSkin = require 'View/UIGuild/Skin/GuildAddresSelectSkin';
------------------  fgui require end  ------------------
require "proto/union/bean/UnionSettingInfo"
local GuildCreateSkin = Class("GuildCreateSkin");

function GuildCreateSkin:ctor(com)
    ------------------ fgui事件绑定start ------------------
    self:bindChild(com);
    self.ctl_choice.onChanged:Add(self.onChange_Ctl_choice,self);
    self.btn_blackbackground.onClick:Add(self.onTouch_Btn_blackbackground,self);
    self.btn_replace.onClick:Add(self.onTouch_Btn_replace,self);
    self.btn_createaguild.onClick:Add(self.onTouch_Btn_createaguild,self);
    self.btn_cancel.onClick:Add(self.onTouch_Btn_cancel,self);
    self.btn_choice.onClick:Add(self.onTouch_Btn_choice,self);
    self.btn_choice1.onClick:Add(self.onTouch_Btn_choice1,self);
    self:init();
    ------------------  fgui事件绑定end  ------------------
end

function GuildCreateSkin:bindChild(com)
    ------------------ fgui变量绑定start ------------------
    self.fguiObj = com;
    self.ctl_choice = com:GetController("Ctl_choice");
    self.btn_blackbackground = com:GetChild("Btn_blackbackground");
    self.btn_replace = com:GetChild("Btn_replace");
    self.btn_createaguild = com:GetChild("Btn_createaguild");
    self.btn_cancel = com:GetChild("Btn_cancel");
    self.btn_choice = com:GetChild("Btn_choice");
    self.btn_choice1 = com:GetChild("Btn_choice1");
    self.com_address = GuildAddresSelectSkin.New(com:GetChild("Com_address"));
    self.text_amendmentnotice = com:GetChild("Text_amendmentnotice");
    self.text_close = com:GetChild("Text_close");
    self.text_guildcoin = com:GetChild("Text_guildcoin");
    self.input_guildName = com:GetChild("input_guildName");
    self.ldr_flag = com:GetChild("ldr_flag");
    ------------------  fgui变量绑定end  ------------------
end

function GuildCreateSkin:init() 
    self.text_amendmentnotice.text = getLanguage("GuildInvestment_1040");
    self.btn_replace.text = getLanguage("GuildInvestment_1042");
    self.btn_choice.text = getLanguage("GuildInvestment_1046");
    self.btn_choice1.text = getLanguage("GuildInvestment_1047");
    self.btn_createaguild.text = getLanguage("GuildInvestment_1040");
    self.input_guildName.promptText = getLanguage("GuildInvestment_1043");
    self.closeCallback = nil;
    self:updateView();
    ListenEvent(Config.EventType.Union_CreateInfo_S2C, self.respUnionCreate,self);
end

function GuildCreateSkin:updateView()
    self.flagIdx = logicMgr.GuildManager.getFlagId();
    self.ldr_flag.url = logicMgr.GuildManager.getFlagUrl(self.flagIdx);
    self.input_guildName.text = logicMgr.GuildManager.getUnionName();
    self.ctl_choice.selectedIndex = logicMgr.GuildManager.getUnionSetting().isNeedCheckJoin == 1 and 1 or 0;
    local needNum = logicMgr.GuildManager.getGuildParaIntValue(8);
    self.enough = logicMgr.ItemManager.getCurrencyCountByModelId(CurrencyEnum.GOLD) >= needNum;
    self.text_guildcoin.text = (self.enough and "[color=#FFFFFF]"  or "[color=#FF0000]") .. needNum .. "[/color]";
    self.com_address:updateView();
end

function GuildCreateSkin:OnDestroy()
    ------------------ fgui事件移除start ------------------
    self:beforeDestroy();
    self.ctl_choice.onChanged:Clear();
    self.btn_blackbackground.onClick:Clear();
    self.btn_replace.onClick:Clear();
    self.btn_createaguild.onClick:Clear();
    self.btn_cancel.onClick:Clear();
    self.btn_choice.onClick:Clear();
    self.btn_choice1.onClick:Clear();
    self.com_address:OnDestroy();
    ------------------  fgui事件移除end  ------------------
end

function GuildCreateSkin:onTouch_Btn_blackbackground(obj) 
    if self.closeCallback then self.closeCallback() end
end

function GuildCreateSkin:onTouch_Btn_replace(obj) 
    showUI(PanelNames.UIGuildFlag,self.flagIdx,function(flagIdx) 
        if flagIdx and flagIdx > 0 then
            self.flagIdx = flagIdx;
            self.ldr_flag.url = logicMgr.GuildManager.getFlagUrl(flagIdx);
        end
    end);
end

function GuildCreateSkin:onTouch_Btn_createaguild(obj)
    local unionName = self.input_guildName.text;
    if #unionName == 0 then
        UISysTips.AddMsg(getLanguage("GuildInvestment_1043"));
        return
    end

    if logicMgr.ChatManager.HasFilterBlockWord(unionName) then
        UISysTips.AddMsg(getLanguage("UIFriend_ApplyMsgBlock"))
        return;
    end

    if tonumber(unionName) then
        self.input_guildName.text = "";
        UISysTips.AddMsg(getLanguage("GuildInvestment_1070"));
        return
    end

    if not self.enough then
        UISysTips.AddMsg(getLanguage("GuildInvestment_1071"));
        return
    end

    local infoSetting = UnionSettingInfo.New();
    infoSetting.isNeedCheckJoin = self.ctl_choice.selectedIndex;
    infoSetting.province = self.com_address:getProvince();
    infoSetting.city = self.com_address:getCity();
    infoSetting.flagId = self.flagIdx;
    infoSetting.unionNotice = logicMgr.GuildManager.getUnionNotice();
    unionHandler.reqCreateUnion(unionName,infoSetting);
end

function GuildCreateSkin:onTouch_Btn_cancel(obj)
    if self.closeCallback then self.closeCallback() end
end

function GuildCreateSkin:onTouch_Btn_choice(obj) end

function GuildCreateSkin:onTouch_Btn_choice1(obj) end

function GuildCreateSkin:onChange_Ctl_choice(event) end

------------------  ↑ fgui事件句柄 ↑  ------------------

function GuildCreateSkin:setCloseCallback(func,target)
    self.closeCallback = func;
end

function GuildCreateSkin:respUnionCreate()
    if logicMgr.GuildManager.unionInfo then
        if self.closeCallback then self.closeCallback() end
    else
        UISysTips.AddMsg(getLanguage("GuildInvestment_1072"));
    end
end

function GuildCreateSkin:beforeDestroy() 
    self.closeCallback = nil;
    CloseEvent(Config.EventType.Union_CreateInfo_S2C, self.respUnionCreate,self);
end

return GuildCreateSkin;
